package com.huaxindata.im.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.huaxindata.im.R;

public class CharBar extends View {

    public final static String[] b = {"#","A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z"};
    private int height;
    private int width;
    private Paint paint;
    private float cellWidth;
    // private float cellHeight;
    private float x, y, h;
    private int choose;

    public CharBar(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
        final int dpi = arg0.getResources().getDisplayMetrics().densityDpi;
        paint = new Paint();
        paint.setColor(getResources().getColor(R.color.accent));
        paint.setTypeface(Typeface.DEFAULT);
        paint.setAntiAlias(true);
        paint.setTextSize((float) (14 * (dpi / 160.0f)));
        cellWidth = paint.measureText(b[0]);
        // FontMetrics fm = paint.getFontMetrics();
        // cellHeight = (float) Math.ceil(fm.descent - fm.ascent);
        // System.out.println("CharBar:" + dpi + "," + cellWidth + ","
        // + cellHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (height == 0) {
            height = getHeight();
            width = getWidth();
            h = height / b.length;
        }
        final String[] bs = getShows();
        final int offset = getOffsetY();
        for (int i = 0; i < bs.length; i++) {
            x = (width - cellWidth) / 2;
            y = offset + h + i * h;
            canvas.drawText(bs[i], x, y, paint);
        }
    }

    private int getOffsetY() {
        if (shows == null) {
            return 0;
        } else {
            return (int) (((b.length - shows.length) * h) / 2);
        }
    }

    private String[] getShows() {
        if (shows == null) {
            return b;
        } else {
            return shows;
        }
    }

    String[] shows;

    public void show(String[] shows) {
        this.shows = shows;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        final int oldChoose = choose;
        // final OnTouchingLetterChangedListener listener =
        // onTouchingLetterChangedListener;
        final int c;
        if (shows == null) {
            c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
        } else {
            c = (int) ((y - getOffsetY()) / h);
        }
        switch (action) {
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                choose = -1;//
                // invalidate();
                if (tip != null) {
                    tip.setVisibility(View.GONE);
                }
                break;
            default:
                if (oldChoose != c) {
                    final String[] ref = getShows();
                    if (c >= 0 && c < ref.length) {
                        if (listener != null) {
                            listener.onLetterChanged(ref[c]);
                        }
                        if (tip != null) {
                            tip.setText(ref[c]);
                            tip.setVisibility(View.VISIBLE);
                        }
                        choose = c;
                        // invalidate();
                    }
                }

                break;
        }
        return true;
    }

    private TextView tip;

    private LetterChangeListener listener;

    public interface LetterChangeListener {
        void onLetterChanged(String c);
    }

    /**
     * set the view of tip on letter change
     */
    public void setTip(TextView tip) {
        this.tip = tip;
    }

    /**
     * @param listener
     */
    public void setLetterChangeListener(LetterChangeListener listener) {
        this.listener = listener;
    }

}
